package day0522;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Test9 {
    public static void main(String[] args) throws InterruptedException {
        int c = exam(10, 4);
        System.out.println("回答正确" + c + "回答错误" + (10-c));
    }

    private static int exam(int len, int range) throws InterruptedException {
        String[] questions = new String[len];
        double[] rightAnser = new double[len];
        double[] userAnser = new double[len];
        Scanner s = new Scanner(System.in);
        Random ran = new Random();
        int count = 0;
        for (int i = 0; i < len; ) {
            Thread.sleep(600);
            int n =  ran.nextInt(4);
            int num1 = ran.nextInt(range)+1;
            int num2 = ran.nextInt(range)+1;
            switch (n) {
                case 0:
                    boolean rs = judge(questions, i, num1 + "+" + num2 + "=");
                    if (rs == true) {
                        continue;
                    }
                    questions[i] = num1 + "+" + num2 + "=";
                    rightAnser[i] = num1 + num2;
                    break;
                case 1:
                    boolean rs1 = judge(questions, i, num1 + "-" + num2 + "=");
                    if (rs1 == true) {
                        continue;
                    }
                    questions[i] = num1 + "-" + num2 + "=";
                    rightAnser[i] = num1 - num2;
                    break;
                case 2:
                    boolean rs2 = judge(questions, i, num1 + "*" + num2 + "=");
                    if (rs2 == true) {
                        continue;
                    }
                    questions[i] = num1 + "*" + num2 + "=";
                    rightAnser[i] = num1 * num2;
                    break;
                case 3:
                    boolean rs3 = judge(questions, i, num1 + "/" + num2 + "=");
                    if (rs3 == true) {
                        continue;
                    }
                    questions[i] =  (double)num1 + "/" +  (double)num2 + "=";
                    double rAns = (double)num1 / (double)num2;
                    rAns = (double)Math.round(rAns*100)/100;
                    rightAnser[i]=rAns;
                    break;
            }
            System.out.println(i+"::"+Arrays.toString(questions));
            double ans=s.nextDouble();
            userAnser[i]=ans;
            if (rightAnser[i]==userAnser[i]) {
                count++;
            }else{
                System.out.println(questions[i]+"用户答案："+userAnser[i]+"，正确答案是"+rightAnser[i]);
            }
            i++;

        }
        return count;
    }

    private static boolean judge(String[] questions, int i, String ques) {
        for (int j = 0; j < i; j++) {
            if (ques.equals(questions[j])) {
                return true;
            }

        }
        return false;
    }

}
